//
//  ENDynamicPlistManager.m
//  EverydayNews
//
//  Created by 李沛池 on 2017/8/9.
//  Copyright © 2017年 Godlike. All rights reserved.
//

#import "ENDynamicPlistManager.h"
#import "LPCDefine.h"

//存储在本地的动态文件名
#define FileName_MyDynamic @"MyDynamic.plist"
//存储在本地的动态文件目录
#define FilePath_MyDynamic [PATH_Document stringByAppendingPathComponent:FileName_MyDynamic]

@implementation ENDynamicPlistManager

//读取所有动态
+ (NSArray *)loadAllDynamic
{
    //Document目录
    NSLog(@"FilePath_MyDynamic = %@",FilePath_MyDynamic);
    //文件管理
    NSFileManager* fm = [NSFileManager defaultManager];
    //如果文件不存在 则创建文件
    if (![fm fileExistsAtPath:FilePath_MyDynamic]) {
        //创建一个plist文件
        [fm createFileAtPath:FilePath_MyDynamic contents:nil attributes:nil];
    }
    //获取已存储的动态数据
    NSArray *arrayDynamics = [NSArray arrayWithContentsOfFile:FilePath_MyDynamic];
    
    return arrayDynamics;
}

//保存动态
+ (void)saveDynamic:(NSString *)dynamic
{
    //最终保存到本地的数组
    NSMutableArray *arraySavePlist;
    
    NSArray *arrayDynamics = [self loadAllDynamic];
    //如果无数据
    if (arrayDynamics.count == 0 || arrayDynamics == nil) {
        arraySavePlist = [[NSMutableArray alloc] init];
    }else {
        arraySavePlist = [[NSMutableArray alloc] initWithArray:arrayDynamics];
    }
    [arraySavePlist addObject:dynamic];
    //写入数据
    if ([arraySavePlist writeToFile:FilePath_MyDynamic atomically:YES]) {
        
    }
}

//清空动态
+ (BOOL)clearDynamic
{
    NSFileManager* fm = [NSFileManager defaultManager];
    //如果存在则删除文件
    if ([fm fileExistsAtPath:FilePath_MyDynamic]) {
        return [fm removeItemAtPath:FilePath_MyDynamic error:nil];
    }else {
        return YES;
    }
    return YES;
}


@end
